<?php

    include_once('app/connection.php');
    include_once('app_sv/check_user.php');
    
    $sql = mysql_query("SELECT * FROM hocky WHERE HOCKY=(SELECT MAX(HOCKY) FROM hocky WHERE NAM=(SELECT MAX(NAM) FROM hocky))
        AND NAM=(SELECT MAX(NAM) FROM hocky)");
    $row = mysql_fetch_assoc($sql);    
    $hocky = $row['HOCKY'];
    $nam = $row['NAM'];    
    
    if(isset($_POST['dk_mh']))
    {
        $mssv = $_SESSION['s_user'];
        
        //$sql = mysql_query("DELETE FROM dangky WHERE mssv=$mssv");
        
        $array = $_POST['dk_mh'];
        $count = count($array);
        for($i = 0; $i < $count; $i++) 
        {
            $v = explode(".", $array[$i]);
            
            $sql = mysql_query("INSERT INTO dangky (MSSV, MAMONHOC, MALOP, HOCKY, NAM) 
                VALUES ('$mssv', '$v[0]', '$v[1]', '$hocky', '$nam')");
        }
		
		$sql = mysql_query("SELECT dangky.MAMONHOC,dangky.MALOP,TENMONHOC,CA,THU FROM dangky, lop, monhoc 
                    WHERE lop.MALOP = dangky.MALOP AND lop.MAMONHOC = dangky.MAMONHOC AND lop.HOCKY = dangky.HOCKY 
                    AND lop.NAM = dangky.NAM AND lop.MAMONHOC = monhoc.MAMONHOC AND dangky.NAM = (SELECT MAX(NAM) FROM dangky) 
                    AND dangky.HOCKY = (SELECT MAX(HOCKY) FROM dangky WHERE NAM = (SELECT MAX(NAM) FROM dangky)) AND MSSV = '$mssv'");
                    
			$ar = array();
			$ca = "";
			$montrung = "";
			while($rw = mysql_fetch_array($sql))
			{
				$ar[] = array("MALOP"=>$rw['MALOP'], "MAMONHOC"=>$rw['MAMONHOC'], "TENMONHOC"=>$rw['TENMONHOC'], 
				"CA"=>$rw['CA'], "THU"=>$rw['THU']);
			}
			$counts = count($ar); // so phan tu mang 1 chieu cua mang 2 chieu
			
			for($i=0; $i < $counts-1;$i++)
			{
				for($j=$i+1; $j<$counts; $j++)
				{
					if( (($ar[$i]['CA'] == $ar[$j]['CA']) || (preg_match("/".$ar[$i]['CA']."/",$ar[$j]['CA'],$match) || 
					preg_match("/".$ar[$j]['CA']."/",$ar[$i]['CA'],$match))) && ($ar[$i]['THU'] == $ar[$j]['THU']) )
					{
						$montrung .= "Lớp: ".$ar[$i]['MAMONHOC'].".".$ar[$i]['MALOP']." - ".$ar[$i]['TENMONHOC']."<br/>"
						."Lớp: ".$ar[$j]['MAMONHOC'].".".$ar[$j]['MALOP']." - ".$ar[$j]['TENMONHOC']."<br />Trùng cùng thứ: ".$ar[$i]['THU']
						." ca:".$ar[$i]['CA']." và ca ".$ar[$j]['CA']."<br/><br/>";
					}
				}
			}
        
        if($montrung != "")//if($count1 < $count2)
        {
            echo "<i>Bạn đã đăng ký trùng: <br/>".$montrung;// ca và thứ ở một số môn, vui lòng đăng ký lại</i>";
            $sql = mysql_query("DELETE FROM dangky WHERE MSSV = '$mssv' AND HOCKY = '$hocky' AND NAM = '$nam'");            
        }
            
        else echo "<script>window.location='?page=trangthai_dkmh';</script>";
    }
?>

<style>
    .danhsach tr:first-child{background: #247CB9;}
    .danhsach tr:nth-child(odd){background: #fff;}
    .danhsach tr:nth-child(odd){background: #ccc;}
</style>

<?php    
    if($is_sv)
    {
        echo "<center><h2> ĐĂNG KÝ MÔN HỌC </h2>";
        echo "<h3> HỌC KỲ ".$row['HOCKY']." NĂM ".$row['NAM']." </h3></center>";
        
?>
            
            <div style="width: 720px; min-height: 400px; overflow-y: scroll; margin: 0 auto;">
            <form name="dkmh" method="POST" action="">
            <center><table class="danhsach">
            
                <tr style="background: #247CB9;">
                    <td style="text-align: center; color: #fff;">Mã lớp</td>
                    <td style="text-align: center; color: #fff;">Tên Môn học</td>
                    <td style="text-align: center; color: #fff;">Tên Giảng viên</td>
                    <td style="width: 50px;text-align: center; color: #fff;">Ca</td>
                    <td style="text-align: center; color: #fff;">Thứ</td>                    
                    <td style="text-align: center; color: #fff;">Phòng</td>
                    <td style="text-align: center; color: #fff;">Tối đa</td>
                    <td style="text-align: center; color: #fff;">Đã ĐK</td>
                    <td style="text-align: center; color: #fff;">Chọn</td>
                </tr>
        <?php
                $sql = mysql_query("SELECT * FROM lop, giaovien, monhoc WHERE monhoc.MAMONHOC=lop.MAMONHOC 
                    AND lop.MAGIAOVIEN=giaovien.MAGIAOVIEN AND lop.HOCKY='$hocky' AND NAM='$nam' AND lop.TRANGTHAI='1'
                    ORDER BY lop.MAMONHOC, lop.MALOP ");
                while($rows = mysql_fetch_assoc($sql))
                {
        ?>
                     <tr>
                     <td style="text-align: center;"><?php echo $rows['MAMONHOC'].'.'.$rows['MALOP']; ?></td>
                     <td><?php echo $rows['TENMONHOC']; ?></td>
                     <td><?php echo $rows['HOTEN']; ?></td>
                     <td style="text-align: center;"><?php echo $rows['CA']; ?></td>
                     <td style="text-align: center;"><?php echo $rows['THU']; ?></td>
                     <td style="text-align: center;"><?php echo $rows['PHONG']; ?></td>
                     <td style="text-align: center;"><?php echo $rows['TOIDA']; ?></td>
                     
                     <td style="text-align: center;">
                        <?php
                            $s = mysql_query("SELECT * FROM dangky WHERE MAMONHOC='".$rows['MAMONHOC']."' AND MALOP='".$rows['MALOP']."'");
                            $soluong = mysql_num_rows($s);
                            echo $soluong;
                        ?>
                     </td>
                     
                     <td style="text-align: center;">
                        <input type="checkbox" name="dk_mh[]" id="dk_mh" value="<?php 
                            echo $rows['MAMONHOC'].".".$rows['MALOP'].'" ';
                            if($soluong == $rows['TOIDA']) echo "onclick='return false' ";
                            
                            $s = mysql_query("SELECT * FROM dangky WHERE MSSV='".$_SESSION['s_user']."' 
                                AND MAMONHOC='".$rows['MAMONHOC']."' AND MALOP='".$rows['MALOP']."'
                                AND HOCKY='$hocky' AND NAM='$nam'");
                            if(mysql_num_rows($s) != 0) echo "checked";
                        ?> "/>
                     </td>
                     
                     </tr>
            <?php } ?>
            
            </table></center>            
            </div>
            
            <center><input type="submit" value="Đăng Ký" /></center>
            </form>
        
<?php
            
    }    
    else {
        echo "Bạn không có quyền thực hiện chức năng này!";
    }
?>
